﻿<?php
require_once(dirname(__FILE__)."/../../libs/login/login.php");
require_once(dirname(__FILE__)."/../../libs/dbclass/dbconnect.php");
require_once(dirname(__FILE__)."/../../include/fun.php");
require_once(dirname(__FILE__)."/../../admin/libs/backup_class.php");
$lg=new login();
$lg->checklogin("admin_member");//检验是否登录
 //创建目录
$basepath=dirname(__FILE__).'/../../backupdata/';
if(!is_dir($basepath)){ 
	mkdir($basepath,0777);	
	chmod($basepath,0777); //linux 需要权限
}

// ajax备份数据库
if(!empty($_POST) && $_POST['active']=='backup'){  
	$b=new mysqlBackup(); //备份数据库
	$mysql_data=$b->backup();  //获取备份数据
	$filename=$basepath.date('Ymd').'.sql';
	file_put_contents($filename,$mysql_data);
	echo '1';die;
}

// ajax删除文件
if(!empty($_POST) && $_POST['active']=='delete'){  

   $filename=$_POST['filename'];
   if(empty($filename)){
		echo '-1';                  //没权限
		exit;
	}
   $mysql_file=$basepath.$filename;
   if(file_exists($mysql_file)){
		@unlink($mysql_file);
		echo '1';
		exit;	
	}else{
		echo '-1';                  //没权限
		exit;
	}
	
}
	


$filearr=array();
if (is_dir($basepath)){
    if ($dh = opendir($basepath)){
        while (($file = readdir($dh)) !== false){
				$filepart=pathinfo($file);
				$type=$filepart['extension']; //后缀名
				if($type=='sql'){			//过滤格式	
					$filearr[]=$file;
					 rsort($filearr);  //从大大小排序
				}   
        }
        closedir($dh);
    }
}

$newst=$filearr[0];
//$newst_time=substr($newst,0,strrpos($newst,'.'));//最新时间
$newst_time=basename($newst,'sql');//最近备份时间 例如20130824

$backupurl='http://'.$_SERVER['HTTP_HOST'].'/backupdata/';
$mcrypt_str='82h427sdi44sps';
$path="数据库备份还原";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台管理系统</title>
<link rel="stylesheet" type="text/css" href="../../plug/swfupload/uploadify.css">
<link href="../css/css.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../js/jquery.js"></script>
<script>
function backupMysql(){
	//alert('111111');
	$.post('backup.php',{active:'backup'},function(data){
		var d=data.trim();
		 if(d=='1'){
			alert('备份成功');
			location.href='backup.php';
			}
	})	
}


/*
* 删除文件
* 
*/	
function delMysql(filename){
  if(window.confirm("你确认要删除吗？")){

	 $.post('backup.php',{active:'delete',filename:filename},function(data){
		var d=data.trim();
		 if(d=='1'){
			alert('删除成功');
			location.href='backup.php';
		}else if(d==-1){
			alert('文件不存在');
			location.href='backup.php';
		}
	})	
	
  }else{
	  return false;
  }

}
</script>

</head>
<body style="background:#fff"> 
    <div class="content">
        <div class="right">
        
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                  <td width="100%" height="24" class="top_head">&nbsp;</td>
                </tr>
                <tr>
                  <td valign="top">
                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                          <td width="1%" align="right"  height="45">&nbsp;</td>
                          <td width="99%" >
                           <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td width="55%" style="padding-left:20px;padding-top:10px;"><img src="../images/head.gif" width="13" height="11"><b><?php echo $path;?></b><font></font></td>
                              <td width="45%" align="right">&nbsp;</td>
                            </tr>
                          </table>
                          </td>
                        </tr>
                      </table>
                  </td>
                </tr>
                  <tr align="center">
                  <td  height="50" >
                        <div class="backup" ><h4>最后备份时间为:<?php echo date('Y-m-d',strtotime($newst_time))?></h4> &nbsp;&nbsp;&nbsp;<span> <bdo><input type="button" name="backup"  value="点击备份" onclick="backupMysql()"  class="buttonstyle" style="background-color: #1399E6;border: 1px solid #0D87CE;width: 75px;height: 30px;text-align: center;line-height: 30px;margin-right: 5px;font-size: 14px;"> </bdo></span></div>
                  </td>
                  <style>
                    .backup h4{ display:inline};
                  </style>
                </tr>
                 <tr align="center">
                  <td  height="30" >
                   </td>
                 </tr>
         </table>
           
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                 <td valign="middle" align="center" >
                    <table width="92%"   border="1px" bordercolor="#ccc" cellspacing="0px" style="border-collapse:collapse">
                       <thead align="center" bgcolor="#EEEEEE">
                         <tr>
                            <th width="4%"  align="center" height="30" >序号</th>
                            <th width="8%"  align="center"><strong>备份文件</strong></th>
                            <th width="50%" align="center"><strong>操作</strong></th>
                        </tr>
                      </thead>
                      <tbody>
                    <?php if(count($filearr)>0){
                        $index=1;
                        foreach($filearr as $val){?>
                          <tr align="center" bgcolor="#FFFFFF">
                              <td height="40" align="center"><?php echo $index++;?></td>            
                              <td height="40" align="center"><?php echo $val?></td>
                              <td height="40"  align="center"  >
                                <bdo>
                                <a href="download.php?name=<?php echo $val ?>&dir=<?php echo base64_encode($basepath)?>" target="_blank"><input type="submit" name="button7" id="button7" value="下载" /></a>
                              </bdo>
                                <bdo>
                                <input type="submit" name="button9" id="button9" value="删除" onclick="delMysql('<?php echo $val?>')"/>
                              </bdo></td>
                            </tr>
                    <?php }
                        }else{?>
                            <tr align="center" bgcolor="#FFFFFF">
                                  <td height="40" align="center" colspan="9">亲，你忘记备份了吗?赶紧备份哦！</td>
                            </tr>
                   <?php }?>
                      </tbody>
                  </table>
                </td>
              </tr>
         </table>
        
        </div>
    </div>
</body>
</html>
